home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 5_5.lha / 5_5 / 5_5c3_6.c < prev    next >
Text File  |  1993-08-08  |  2KB  |  71 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / print out the tree in assembly format
  6. / 8086 version
  7.  
  8. / initialize for the 8086
  9. / set up register bp for the stack
  10. oid setup()
  11.  
  12.    cout << "\tpush bp\n" <<
  13.     "\tmov bp, sp\n";
  14.  
  15.  
  16. / finish things up
  17. oid finishup(stackalloc &stacktop)
  18.  
  19.    cout << "\tmov ax, ";
  20.    stacktop.print(cout) << "\n";
  21.    cout << "\tmov result, ax\n";
  22.    cout << "\tpop bp\n";
  23.  
  24.  
  25. / print out a binary operator followed by the
  26. / source and destination
  27. tatic void binop(const tree *head,
  28.    stackalloc &curloc, const intalloc *cursp)
  29.  
  30.    assemblyprint(head->left, curloc, cursp);
  31.    stackalloc newloc(cursp);
  32.    assemblyprint(head->right, newloc, cursp);
  33.    cout << "\tmov ax,";
  34.    curloc.print(cout) << "\n";
  35.    cout << "\tmov bx,";
  36.    newloc.print(cout) << "\n";
  37.  
  38.    switch (head->type)
  39. {
  40. case PLUS:  cout << "\tadd ax, bx\n"; break;
  41. case MUL:   cout << "\tmul bx\n"; break;
  42. case DIV:   cout << "\tmov dx, 0\n" <<
  43.             "\tdiv bx\n"; break;
  44. case MINUS: cout << "\tsub ax, bx\n"; break;
  45. }
  46.  
  47.    cout << "\tmov ";
  48.    curloc.print(cout) << ", ax\n";
  49.  
  50.  
  51. / print out a unary operator followed
  52. / by the destination
  53. tatic void negop(const tree *head,
  54.    stackalloc &curloc, const intalloc *cursp)
  55.  
  56.    stackalloc newloc(cursp);
  57.    assemblyprint(head->left, newloc, cursp);
  58.    cout << "\tmov ax, ";
  59.    newloc.print(cout) << "\n";
  60.    cout << "\tneg ax";
  61.    cout << "\tmov ";
  62.    curloc.print(cout) << ", ax\n";
  63.  
  64.  
  65. / store a number node at the current location
  66. tatic void svnumber(tree* head, stackalloc &curloc)
  67.  
  68.    cout << "\tmov ";
  69.    curloc.print(cout) << ", " << head->value << "\n";
  70.  
  71.